/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.report.mapper.statistics;

import com.koron.report.bean.StatisticsBean;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 安全工器具检查合格率
 */
public interface CheckPassRateSafetyToolsMapper {

    /**
     * 安全工器具检查合格总数 统计：按检查人员
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  d.responsible_employee_code AS code, d.responsible_employee_name AS codeName, " +
            "        count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where r.normal = 1" +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        AND d.last_check_date between #{startDate} and #{stopDate} group by d.responsible_employee_code, d.responsible_employee_name" +
            "</script>")
    List<StatisticsBean> statisticsQualifiedTotalPerson(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                                        @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查总数 统计：按检查人员
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  d.responsible_employee_code AS code, d.responsible_employee_name AS codeName, " +
            "        count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where d.last_check_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        group by d.responsible_employee_code, d.responsible_employee_name" +
            "</script>")
    List<StatisticsBean> statisticsTotalPerson(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                               @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查合格总数 统计：按部门
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  d.dept_code AS code, d.dept_name AS codeName, " +
            "        count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where r.normal = 1" +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        AND d.last_check_date between #{startDate} and #{stopDate} group by d.dept_code, d.dept_name" +
            "</script>")
    List<StatisticsBean> statisticsQualifiedTotalDept(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                                      @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查总数 统计：按部门
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  d.dept_code AS code, d.dept_name AS codeName, " +
            "        count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where d.last_check_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        group by d.dept_code, d.dept_name" +
            "</script>")
    List<StatisticsBean> statisticsTotalDept(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                             @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查合格总数 统计：按公司
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  d.corp_code AS code," +
            "        count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where r.normal = 1" +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        AND d.last_check_date between #{startDate} and #{stopDate} group by d.corp_code" +
            "</script>")
    List<StatisticsBean> statisticsQualifiedTotal(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                                  @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查总数 统计：按公司
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  d.corp_code AS code," +
            "        count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where d.last_check_date between #{startDate} and #{stopDate} " +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        group by d.corp_code" +
            "</script>")
    List<StatisticsBean> statisticsTotal(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                         @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查合格总数 统计：按集团
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where r.normal = 1" +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "        AND d.last_check_date between #{startDate} and #{stopDate}" +
            "</script>")
    Integer statisticsQualifiedTotalBloc(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                         @Param("deviceTypeId") String deviceTypeId);

    /**
     * 安全工器具检查总数 统计：按集团
     *
     * @param startDate
     * @param stopDate
     * @param deviceTypeId 设备类型id,引用数据字典
     * @return
     */
    @Select("<script>" +
            "select  count(d.id) AS value" +
            "        from tbl_device as d inner join device_check_relation as r on (d.id = r.device_id)" +
            "        where d.last_check_date between #{startDate} and #{stopDate}" +
            "        <if test='deviceTypeId != null'> AND d.device_type_id = #{deviceTypeId}</if>" +
            "</script>")
    Integer statisticsTotalBloc(@Param("startDate") Date startDate, @Param("stopDate") Date stopDate,
                                @Param("deviceTypeId") String deviceTypeId);

}
